import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 动态路由表（为后面做准备-后台返回权限信息筛选的事情最后两天将）
// 先把8个路由页面先配置好
import approvalsRouter from './modules/approvals'
import departmentsRouter from './modules/departments'
import employeesRouter from './modules/employees'
import permissionRouter from './modules/permission'
import attendancesRouter from './modules/attendances'
import salarysRouter from './modules/salarys'
import settingRouter from './modules/setting'
import socialRouter from './modules/social'
export const asyncRoutes = [
  departmentsRouter, // 组织架构 （公司部门关系）
  settingRouter, // 角色管理
  employeesRouter, // 员工管理
  permissionRouter, // 权限管理（权限点）
  approvalsRouter, // 审批
  attendancesRouter, // 考勤
  salarysRouter, // 工资
  socialRouter // 社保
]

// 静态路由表，项目中每个用户都可以访问的功能
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: 'Dashboard', icon: 'dashboard' }
      }
    ]
  },
  {
    path: '/excel',
    component: Layout,
    hidden: true, // 不显示到左侧菜单
    children: [
      {
        path: '',
        name: 'excel',
        component: () => import('@/views/excel/index.vue')
      }
    ]
  },

  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

// 对VueRouter进行实例化
const createRouter = () =>
  new Router({
    // 控制路由滚动行为  滚动到顶部
    scrollBehavior: () => ({ y: 0 }),
    // 组合到一起组成路由表
    routes: [...constantRoutes, ...asyncRoutes] // 配置路由规则 [{},{},{}]
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
